﻿using GN.Pay.Utils;
using GN.Pay.Utils.Reflection;
using GN.Pay.Utils.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.YJPay.Response
{
    /// <summary>
    /// 通知响应
    /// </summary>
    [Serializable]
    class NotifyResponse : AResponse
    {
        private readonly SignDictionary message;

        /// <summary>
        /// 实例化 ANotifyResponse 类新实例
        /// </summary>     
        /// <param name="message"></param>
        public NotifyResponse(IDictionary<string, string> message)
        {
            if (message == null)
            {
                this.message = new SignDictionary(StringComparer.Ordinal);
            }
            else
            {
                this.message = new SignDictionary(message, StringComparer.Ordinal);
            }
        }

        /// <summary>
        /// 获取消息
        /// </summary>
        public SignDictionary Message
        {
            get
            {
                return this.message;
            }
        }

        /// <summary>
        /// 签名验证
        /// </summary>
        /// <returns></returns>
        public bool SignVeryfy(ITradeChannel channel,
            YJPayConfigure config)
        {
            string sign;
            if (!this.message.TryGetValue(sign_Key, out sign) || string.IsNullOrWhiteSpace(sign))
            {
                return false;
            }
            this.message.Remove(sign_Key);
            if (this.message.Count == 0)
            {
                return false;
            }
            string content = this.Message.UrlLinkString() + config.ConfigureInfo.Key;
            this.message[sign_Key] = sign;

            var dd = message.Keys.ToArray();

            Array.Sort(dd);

            var strs = new string[] {"b", "a", "A", "aA", "AA","B" };

            Array.Sort(strs, StringComparer.Ordinal);

            var vDic = (from objDic in message orderby objDic.Key ascending select objDic.Key).ToArray();

            if (vDic != null)
            {

            }


            bool value = Md5Utils.SginVerify(content, sign, config.Encoding);
            return value;
        }

        /// <summary>
        /// 转为实体
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns></returns>
        public T ToEntity<T>() where T : class, new()
        {
            T entity = Activator.CreateInstance<T>();
            var propertys = ReflectionUtils.GetCacheMetaPropertys(typeof(T));
            foreach (var p in propertys)
            {
                Type valueType;
                if (p.SetMemberValue != null
                    && p.MemberType.IsBaseTypeOrNullableDefinitionBaseType(out valueType))
                {
                    string value;
                    if (this.Message.TryGetValue(p.PropertyInfo.Name, out value)
                        && !string.IsNullOrWhiteSpace(value))
                    {
                        p.SetMemberValue(entity, DataConvertUtils.TargetValueConvert(valueType, value));
                    }
                }
            }
            return entity;
        }
    }
}
